package com.online.controller;

import com.faneui.common.utils.MapUtils;
import com.google.common.collect.Lists;
import com.online.dal.dao.OrderDAO;
import com.online.dal.dao.OrderDetailDAO;
import com.online.dal.dao.ProductDAO;
import com.online.dal.model.Order;
import com.online.dal.model.OrderDetail;
import com.online.dal.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("orderDetail")

public class OrderDetailController {

    @Autowired
    private OrderDetailDAO orderdetailDAO;

    @Autowired
    private OrderDAO orderDAO;

    @Autowired
    private ProductDAO productDAO;

    @RequestMapping(value = "doOrderDetail", method = RequestMethod.GET)
    public ModelAndView indexOfAll(@RequestParam Integer orderId ) throws Exception {
        ModelAndView mv = new ModelAndView("orderdetail");

        OrderDetail param = new OrderDetail() ;
        param.setOrder_id(orderId);//将前台传入的值赋值给param对象
        //声明一个OrderDetailList队列对象，通过OrderDetailDAO接口queryByModel()方法查询
        List<OrderDetail> OrderDetailList = orderdetailDAO.queryByModel(param) ;
        List<Map<String,Object>> list = Lists.newArrayList() ;

        for(OrderDetail orderDetail:OrderDetailList){//循环OrderDetailList赋值给orderDetail对象
            Map<String,Object> map = MapUtils.parseMap(orderDetail) ;//orderDetail赋值给map对象
            //循环得到的product_id,通过ProductDAO接口queryById()方法查询到商品信息赋值给product对象
            Product product = productDAO.queryById(orderDetail.getProduct_id()) ;
            //通过put()方法对集合对象添加一个 键-值对
            map.put("productName",product.getName()) ;
            list.add(map) ;//通过add()方法对集合对象添加map对象
        }
        //ModelAndView的addObject()方法可以将对象添加到模型中，并且可以添加对象使用的参数名称生成模型
        mv.addObject("orderDetailList", list);
        return mv;
    }

}
